//
//  InternetDataRequest.m
//  自封装头文件
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 Wang Jianwei. All rights reserved.
//

#import "InternetDataRequest.h"

@implementation InternetDataRequest
+ (void)getDataByString:(NSString *)urlSting BodyDic:(NSDictionary *)bodyDic WithDataBlock:(void (^)(id))dataBlock
{
    //1.字符串的转码
    urlSting = [urlSting stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlSting]];
    //2.创建管理者对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //3.设置允许请求的类别
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", nil];
    //4.开始请求
    if (!bodyDic) {
        [manager GET:urlSting parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            dataBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败");
        }];
    }
    else
    {
        [manager POST:urlSting parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            dataBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败");
        }];
    }
}
+ (NSDictionary *)changeStringToDictionary:(NSString *)string
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSArray *array = [string componentsSeparatedByString:@"&"];
    for (NSString *str in array) {
        NSArray *tempArray = [str componentsSeparatedByString:@"="];
        [dic setObject:tempArray[1] forKey:tempArray[0]];
    }
    return dic;
}
@end
